package com.dropdb.demo.controller.admin;

import com.dropdb.demo.bean.Ad;
import com.dropdb.demo.bean.vo.BaseData;
import com.dropdb.demo.bean.vo.BaseRespVO;
import com.dropdb.demo.service.PromotionService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qugr
 * @create 2020-06-26 16:53
 */
@RestController
@RequestMapping("admin/ad")
public class AdminPromotionAdController {

    @Autowired
    PromotionService promotionService;

    @RequestMapping("list")
    @RequiresPermissions(value = {"*", "admin:ad:list"}, logical = Logical.OR)
    public BaseRespVO queryAds(Integer page, Integer limit, String name, String content, String sort, String order){
        BaseData baseData = promotionService.queryAds(page, limit, name, content, sort, order);
        return BaseRespVO.ok(baseData);
    }

    @RequestMapping("create")
    @RequiresPermissions(value = {"*", "admin:ad:create"}, logical = Logical.OR)
    public BaseRespVO createAd(@RequestBody Ad ad){
        int insert = promotionService.insertAd(ad);
        if (insert != 0) {
            Ad adVO = promotionService.queryAd(ad.getId());
            return BaseRespVO.ok(adVO);
        }
        return BaseRespVO.error(500,"添加失败");
    }

    @RequestMapping("delete")
    @RequiresPermissions(value = {"*", "admin:ad:delete"}, logical = Logical.OR)
    public BaseRespVO deleteAd(@RequestBody Ad ad){
        int delete = promotionService.deleteAd(ad.getId());
        if (delete != 1) {
            return BaseRespVO.error(500,"删除失败");
        }
        return BaseRespVO.ok();
    }

    @RequestMapping("update")
    @RequiresPermissions(value = {"*", "admin:ad:update"}, logical = Logical.OR)
    public BaseRespVO updateAd(@RequestBody Ad ad){
        int update = promotionService.updateAd(ad);
        if (update != 1) {
            return BaseRespVO.error(500,"更新失败");
        }
        Ad queryAd = promotionService.queryAd(ad.getId());
        return BaseRespVO.ok(queryAd);
    }

}
